<?php
// +----------------------------------------------------------------------
// | 搜索功能区组件
// +----------------------------------------------------------------------
declare (strict_types=1);

namespace addons\BuilderPlus\components;

class Search
{
    protected $config = [
        'field'            => '',
        'title'            => '',
        'type'             => 'input',
        'condition'        => '=',
        'optionValue'      => [],
        'optionGroupValue' => [],
        'linkageUrl'       => '',
        'dateType'         => 'date',
        'dateRange'        => true,
        'relationClass'    => '',
        'relationField'    => ''
    ];

    public function __construct(string $field, string $title)
    {
        $this->config['field'] = $field;
        $this->config['title'] = $title;
    }

    public function field(string $field)
    {
        $this->config['field'] = $field;
        return $this;
    }

    public function title(string $title)
    {
        $this->config['title'] = $title;
        return $this;
    }

    public function type(string $type)
    {
        $this->config['type'] = $type;
        return $this;
    }


    public function condition(string $condition)
    {
        $this->config['condition'] = $condition;
        return $this;
    }

    public function optionValue($optionValue)
    {
        $this->config['optionValue'] = $optionValue;
        return $this;
    }

    public function optionGroupValue(array $optionGroupValue)
    {
        $this->config['optionGroupValue'] = $optionGroupValue;
        return $this;
    }


    public function linkageUrl(string $linkageUrl)
    {
        $this->config['linkageUrl'] = $linkageUrl;
        return $this;
    }


    public function dateType(string $dateType)
    {
        $this->config['dateType'] = $dateType;
        return $this;
    }

    public function dateRange(bool $dateRange)
    {
        $this->config['dateRange'] = $dateRange;
        return $this;
    }

    public function relation(string $relationClass, string $relationField)
    {
        $this->config['relationClass'] = $relationClass;
        $this->config['relationField'] = $relationField;
        return $this;
    }

    public function relationClass(string $relationClass)
    {
        $this->config['relationClass'] = $relationClass;
        return $this;
    }

    public function relationField(string $relationField)
    {
        $this->config['relationField'] = $relationField;
        return $this;
    }

    public function render()
    {
        return $this->config;
    }

    public function __call($name, $arguments)
    {
        if (isset($this->config[$name])) {
            $this->config[$name] = $arguments;
        }
        return $this;
    }
}